package ldh;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.zip.DataFormatException;

import ldh.serialize.LazyMethod;
import ldh.serialize.SerializedConfig;
import ldh.serialize.SerializedItem;
import ldh.serialize.type.BooleanSerialized;
import ldh.serialize.type.CharSerialized;
import ldh.serialize.type.DateSerialized;
import ldh.serialize.type.EnumSerialized;
import ldh.serialize.type.IterableSerialized;
import ldh.serialize.type.MapSerialized;
import ldh.serialize.type.ShortSerialized;
import ldh.serialize.type.StrSerialized;
import ldh.serialize.util.SerializedUtil;

public class LdhUserSerialize  {

	private static List<SerializedItem<LdhUser, ?>> data = new ArrayList<SerializedItem<LdhUser, ?>>();
	
//	static{
//		data.add(new SerializedItem<LdhUser, Date>(new CreateTime(), new DateSerialized()));
//		data.add(new SerializedItem<LdhUser, Short>(new Age(), new ShortSerialized()));
//		data.add(new SerializedItem<LdhUser, String>(new Name(), new StrSerialized()));
//		data.add(new SerializedItem<LdhUser, Character>(new Test(), new CharSerialized()));
//		data.add(new SerializedItem(new Address(), new IterableSerialized( new StrSerialized())));
//		data.add(new SerializedItem<LdhUser, String>(new Id(), new StrSerialized()));
//		data.add(new SerializedItem<LdhUser, Boolean>(new IsProtected(), new BooleanSerialized()));
//		data.add(new SerializedItem(new Props(), new MapSerialized(new StrSerialized(), new StrSerialized())));
//		data.add(new SerializedItem<LdhUser, SexEnum>(new Sex(), new EnumSerialized(SexEnum.class)));
//	}
	
//	static{
//		data.add(new SerializedItem<LdhUser, Date>(SerializedUtil.getField(LdhUser.class, "createTime"), new DateSerialized()));
//		data.add(new SerializedItem<LdhUser, Short>(SerializedUtil.getField(LdhUser.class, "age"), new ShortSerialized()));
//		data.add(new SerializedItem<LdhUser, String>(SerializedUtil.getField(LdhUser.class, "name"), new StrSerialized()));
//		data.add(new SerializedItem<LdhUser, Character>(SerializedUtil.getField(LdhUser.class, "test"), new CharSerialized()));
//		data.add(new SerializedItem(SerializedUtil.getField(LdhUser.class, "address"), new IterableSerialized( new StrSerialized())));
//		data.add(new SerializedItem<LdhUser, String>(SerializedUtil.getField(LdhUser.class, "id"), new StrSerialized()));
//		data.add(new SerializedItem<LdhUser, Boolean>(SerializedUtil.getField(LdhUser.class, "isProtected"), new BooleanSerialized()));
//		data.add(new SerializedItem(SerializedUtil.getField(LdhUser.class, "props"), new MapSerialized(new StrSerialized(), new StrSerialized())));
//		data.add(new SerializedItem<LdhUser, SexEnum>(SerializedUtil.getField(LdhUser.class, "sex"), new EnumSerialized(SexEnum.class)));
//	}
	
	public static byte[] toBytes(LdhUser ldhUser) throws IOException {
		byte[] bytes = SerializedUtil.toBytes(SerializedConfig.getSerializeItems(ldhUser.getClass()), ldhUser);
//		bytes = CompressionUtil.compress(bytes, Level.BEST_SPEED);
		return bytes;
	}
	
	public static LdhUser toObject(byte[] bytes) throws IOException, DataFormatException {
		LdhUser ldhUser = new LdhUser();
//		byte[] datas = CompressionUtil.decompress(bytes);
		return SerializedUtil.toObject(SerializedConfig.getSerializeItems(ldhUser.getClass()), ldhUser, bytes);
	}
	
	private static class CreateTime implements LazyMethod<LdhUser, Date> {
		
		@Override
		public Date getValue(LdhUser ldhUser) {
			return ldhUser.getCreateTime();
		}

		@Override
		public void setValue(LdhUser ldhUser, Date t) {
			ldhUser.setCreateTime(t);
		}

		@Override
		public Class getClassType() {
			return Date.class;
		}
		
		public String getPropertyName() {
			return "createTime";
		}
		
	}
	
	private static class Age implements LazyMethod<LdhUser, Short> {
		
		@Override
		public Short getValue(LdhUser ldhUser) {
			return ldhUser.getAge();
		}

		@Override
		public void setValue(LdhUser ldhUser, Short t) {
			ldhUser.setAge(t);
		}

		@Override
		public Class getClassType() {
			return Short.class;
		}
		
		public String getPropertyName() {
			return "age";
		}
		
	}
	
	private static class Name implements LazyMethod<LdhUser, String> {
		
		@Override
		public String getValue(LdhUser ldhUser) {
			return ldhUser.getName();
		}

		@Override
		public void setValue(LdhUser ldhUser, String t) {
			ldhUser.setName(t);
		}

		@Override
		public Class getClassType() {
			return String.class;
		}
		
		public String getPropertyName() {
			return "name";
		}
		
	}
	
	private static class Test implements LazyMethod<LdhUser, Character> {
		
		@Override
		public Character getValue(LdhUser ldhUser) {
			return ldhUser.getTest();
		}

		@Override
		public void setValue(LdhUser ldhUser, Character t) {
			ldhUser.setTest(t);
		}

		@Override
		public Class getClassType() {
			return Character.class;
		}
		
		public String getPropertyName() {
			return "test";
		}
		
	}
	
	private static class Address implements LazyMethod<LdhUser, List<String>> {
		
		@Override
		public List<String> getValue(LdhUser ldhUser) {
			return ldhUser.getAddress();
		}

		@Override
		public void setValue(LdhUser ldhUser, List<String> t) {
			ldhUser.setAddress(t);
		}

		@Override
		public Class getClassType() {
			return List.class;
		}
		
		public String getPropertyName() {
			return "address";
		}
		
	}
	
	private static class Id implements LazyMethod<LdhUser, String> {
		
		@Override
		public String getValue(LdhUser ldhUser) {
			return ldhUser.getId();
		}

		@Override
		public void setValue(LdhUser ldhUser, String t) {
			ldhUser.setId(t);
		}

		@Override
		public Class getClassType() {
			return String.class;
		}
		
		public String getPropertyName() {
			return "id";
		}
		
	}
	
	private static class IsProtected implements LazyMethod<LdhUser, Boolean> {
		
		@Override
		public Boolean getValue(LdhUser ldhUser) {
			return ldhUser.getIsProtected();
		}

		@Override
		public void setValue(LdhUser ldhUser, Boolean t) {
			ldhUser.setIsProtected(t);
		}

		@Override
		public Class getClassType() {
			return Boolean.class;
		}
		
		public String getPropertyName() {
			return "isProtected";
		}
		
	}
	
	private static class Props implements LazyMethod<LdhUser, Map<String, String>> {
		
		@Override
		public Map<String, String> getValue(LdhUser ldhUser) {
			return ldhUser.getProps();
		}

		@Override
		public void setValue(LdhUser ldhUser, Map<String, String> t) {
			ldhUser.setProps(t);
		}

		@Override
		public Class getClassType() {
			return Map.class;
		}
		
		public String getPropertyName() {
			return "props";
		}
		
	}
	
	private static class Sex implements LazyMethod<LdhUser, SexEnum> {
		
		@Override
		public SexEnum getValue(LdhUser ldhUser) {
			return ldhUser.getSex();
		}

		@Override
		public void setValue(LdhUser ldhUser, SexEnum t) {
			ldhUser.setSex(t);
		}

		@Override
		public Class getClassType() {
			return SexEnum.class;
		}
		
		public String getPropertyName() {
			return "sex";
		}
		
	}
	

}
